import { Toast } from 'antd-mobile'
import _ from 'lodash'
import { createAction, NavigationActions, URL } from '../utils'
import { get } from '../utils/fetch'

export default {
  namespace: 'login',
  state: {
    user: ''
  },
  reducers: {
    addUser(state, { payload }) {
      const user = payload
      return {
        ...state,
        user: {
          ...user
        }
      }
    }
  },
  effects: {
    * Login({ payload }, { call, put }) {
      const { username, password } = payload
      const data = yield call(() => get(`${URL}1/login?username=${username}&password=${password}`))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail(dada.error)
        } else {
          yield put(createAction('addUser')(data))
          yield put(NavigationActions.back())
        }
      } else {
        Toast.info('输入账号密码有误！！！')
      }
    }
  },
  subscriptions: {
    setup({ dispatch }) {
    }
  }
}
